#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "print_html.h"

void printHTTPResponseHeader()
{
    printf( "Content-type: text/html;charset=utf-8\n\n");
}

void printHTMLHead(void)
{
    printf( "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n\"http://www.w3.org/TR/html4/loose.dtd\">\n" );
    printf( "<html>\n" );
    printf( "<head>\n" );
    printf( "<title>Sin table</title>\n" );
    printHTMLStyles();
    printf( "</head>\n" );
    printf( "<body>\n" );
}

void printHTMLStyles()
{
    printf( "<link rel=\"stylesheet\" type=\"text/css\" href=\"/css/style_degrees.css\" />\n" );
}

void printHTMLEnd(void)
{
    printf( "</body>\n" );
    printf( "</html>\n" );
}

double StringToDouble(const char * str)
{
    char * pLastChar = NULL;
    double param = strtod(str, &pLastChar);
    if((*str == '\0') || (*pLastChar != '\0'))
    {
        return 0;
    }
    else
    {
        return param;
    }
}


int findIdentifier(char* id, char* str)
{
    int i = 0;
    int j = 0;
    while(i < strlen(str))
    {
        if(id[j] == str[i])
        {
            if(j == strlen(id)-1) 
            {
                if(str[i+1] == '=')
                    break;
            }
            ++j;
            ++i;
        }
        else
        {
            j = 0;
            while(i < strlen(str))
            {
                if(str[i] == '&') 
                {
                    ++i;
                    break;
                }
                ++i;
            }
        }   
    }
    if(i == strlen(str))
    {
        return -1;
    }
    else
    {
        return i;
    }
}

int getValueString(char* id, char* str, char* valueString)
{
    int i = findIdentifier(id, str);
    if(i != -1)
    {
        int j = 0;
        i = i + 2;
        while((str[i] != '&') && (i <= (strlen(str)-1)))
        {
            valueString[j] = str[i];
            ++j;
            ++i;
        }
        valueString[j] = '\0';
        if(*valueString == '\0')
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return -1;
    }
}

double getValue(char* id, char* str)
{
    char valueString[255];
    int findErr = getValueString(id, str, valueString);
    if(findErr == 1)
    {
        printf("<p>Value is empty!</p>\n");
        return -1;   
    }
    else if(findErr == -1)
    {
        //printf("Identifier '%s' was not found!", id);
        return -2;   
    }
    else if(findErr == 0)
    {
        double val = StringToDouble(valueString); 
        if(val == 0)
        {
            printf("<p>Argument '%s' is not a number of step!</p>\n", valueString);
            return -1;
        }
        else
        {
            if(val < 0)
            {
                printf("<p>Argument '%s' must be a positive number!</p>\n", valueString);
                return -1;
            }
            return val;
        }
    }
}
